package com.frank.subject.application.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.frank.common.entity.Result;
import com.frank.common.utils.LoginUtil;
import com.frank.subject.application.convert.SubjectLikedDtoConvert;
import com.frank.subject.application.dto.SubjectLikedDto;
import com.frank.common.entity.PageInfo;
import com.frank.subject.domain.entity.SubjectLikedBo;
import com.frank.subject.domain.service.SubjectLikedDomainService;
import com.google.common.base.Preconditions;
import org.springframework.web.bind.annotation.*;

/**
 * @author Frank Zou
 * @version v0.0.1
 * @date 2025/3/12
 * @description
 */
@RestController
@RequestMapping("/subject/liked")
public class SubjectLikedController {

    private final SubjectLikedDomainService subjectLikedDomainService;

    public SubjectLikedController(SubjectLikedDomainService subjectLikedDomainService) {
        this.subjectLikedDomainService = subjectLikedDomainService;
    }

    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody SubjectLikedDto subjectLikedDto) {
        Preconditions.checkNotNull(subjectLikedDto.getSubjectId(), "题目 ID 不能为空！");
        Preconditions.checkNotNull(subjectLikedDto.getStatus(), "点赞状态不能为空！");
        String loginId = LoginUtil.getLoginId();
        Preconditions.checkNotNull(loginId, "请先登录！");
        subjectLikedDto.setLikeUserId(loginId);
        SubjectLikedBo subjectLikedBo = SubjectLikedDtoConvert.INSTANCE.convertDtoToBo(subjectLikedDto);
        return Result.ok(subjectLikedDomainService.add(subjectLikedBo));
    }


    /**
     * 查询我的点赞数据
     *
     * @return PageResult<SubjectInfoDto>
     */
    @GetMapping("/getSubjectLikedPage")
    public Result<Page<SubjectLikedDto>> getSubjectLikedPage(PageInfo pageInfo) {
        Page<SubjectLikedBo> subjectPage = subjectLikedDomainService.getSubjectLikedPage(pageInfo);
        return Result.ok(SubjectLikedDtoConvert.INSTANCE.convertBoPageToDtoPage(subjectPage));
    }

}
